package com.anno.anno;
import java.lang.annotation.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author cyt
 * @create 2020-07-19 18:03
 */
@SuppressWarnings("all")
@my1(age = 1, id = 15)
public class TestAnnotation {
    @my1(age = 2)
    public int age;


    @my1(age = 2, id = 8)
    public int id;

    @SuppressWarnings("all")
    @my1(value = "1", age = 2, id = 9)
    public void testmethod() {
        System.out.println(id + age);
    }

    public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException {
        /*   new TestAnnotation().testmethod();*/
        TestAnnotation test = new TestAnnotation();
        Class<? extends TestAnnotation> aClass = test.getClass();
        System.out.println("得到字段上的注解  =========================");
        //得到字段上的注解  =========================

        Field age = aClass.getField("id");
        System.out.println(age);

        my1 annotation2 = age.getAnnotation(my1.class);
        System.out.println(annotation2.id());


        System.out.println("得到 方法上的注解  =========================");
        // 得到 方法上的注解  =========================
        Method testmethod = aClass.getMethod("testmethod");
        System.out.println(testmethod);

        my1 annotation1 = testmethod.getAnnotation(my1.class);
        System.out.println(annotation1.school()[0]);

        System.out.println(annotation1.value());

        System.out.println(annotation1.id());
        System.out.println("得到类上的注解  =========================");
        // 得到类上的注解  =========================

        my1 annotation = aClass.getAnnotation(my1.class);

        String value = annotation.value();
        String[] school = annotation.school();

        System.out.println(school[0]);
        System.out.println(annotation.value());

    }


}


/**
 * @author Administrator
 */

// 标记运行时候的注解
@Retention(value = RetentionPolicy.RUNTIME)
// 标记产生文档
@Documented
// 标记作用的范围
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.FIELD})
@interface my1 {
    // 使用 value的时候， 注解的参数可以直接写
    String value() default "value";

    int age();

    // 可以设置默认值
    int id() default 1;

    // 数组类型
    String[] school() default {"洛阳师范学院", "北大"};
}

